package com.uoko.fileupload.util;

import javax.validation.constraints.NotNull;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * <p>
 * 文件流合并
 * </p>
 *
 * @author shaohua
 * Email shaohua@uoko.com
 * created by 2018/6/8
 */
public class StreamUtil {

    /**
     * 文件流合并
     *
     * @param inputStream 输入文件流
     * @param filePath    文件路径
     * @throws Exception
     */
    public static void saveStreamToFile(@NotNull final InputStream inputStream,
                                        @NotNull final String filePath)
            throws Exception {
        OutputStream outputStream = new FileOutputStream(filePath);
        int len = 0;
        byte[] buffer = new byte[1024];
        try {
            while ((len = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, len);
                outputStream.flush();
            }

        } catch (IOException e) {
            e.printStackTrace();
            throw e;
        } finally {
            outputStream.close();
            inputStream.close();
        }
    }
}
